//----------------------------------------------------------- // Purpose: This program demonstrates how multiple queues // can be used to simulate customer arrivals. // Author: John Gauch //----------------------------------------------------------- #include "queue/queue.h" //----------------------------------------------------------- // Main program uses the Queue class to simulate customers. //----------------------------------------------------------- int main() { int NumLines = 5; int NumCustomers = 100; int CurrentTime = 0; Queue *Vendor = new Queue[NumLines]; // Add customers to different queues for (int Customer = 0; Customer < NumCustomers; Customer++) { // Generate random customer data CurrentTime += 5 + random() % 10; int NumItems = 1 + random() % 5; // Search for shortest queue int ShortLine = 0; int ShortLength = Vendor[0].GetCount(); for (int Line = 1; Line < NumLines; Line++) { int Length = Vendor[Line].GetCount(); if (Length < ShortLength) { ShortLine = Line; ShortLength = Length; } } // Add customer to shortest line Vendor[ShortLine].Insert(CurrentTime); Vendor[ShortLine].Insert(NumItems); cout << "Insert " << ShortLine << " " << CurrentTime << " " << NumItems << endl; Vendor[ShortLine].Print(); // Find customer with earliest time int EarlyLine = ShortLine; int EarlyTime = Vendor[ShortLine].GetFront(); for (int Line = 0; Line < NumLines; Line++) { if ((Vendor[Line].GetCount() > 0) && (Vendor[Line].GetFront() < EarlyTime)) { EarlyLine = Line; EarlyTime = Vendor[Line].GetFront(); } } // Process customer orders if (random() % 2 == 0) { int Time = 0; int Items = 0; Vendor[EarlyLine].Print(); Vendor[EarlyLine].Remove(Time); Vendor[EarlyLine].Remove(Items); cout << "Remove " << EarlyLine << " " << Time << " " << Items << endl; } } // Print all customer lines for (int Line = 0; Line < NumLines; Line++) { cout << "Line " << Line << " "; Vendor[Line].Print(); } cout << endl; }